home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb / sprite / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-28  |  47.1 KB  |  1,879 lines

  1. /* Top level for GDB, the GNU debugger.
  2.    Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. GDB is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GDB is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GDB; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include "defs.h"
  22. #include "command.h"
  23. #include "param.h"
  24.  
  25. #ifdef USG
  26. #include <sys/types.h>
  27. #include <unistd.h>
  28. #endif
  29.  
  30. #include <sys/file.h>
  31. #include <setjmp.h>
  32. #include <signal.h>
  33. #include <sys/param.h>
  34. #include <sys/stat.h>
  35.  
  36. #ifdef SET_STACK_LIMIT_HUGE
  37. #include <sys/time.h>
  38. #include <sys/resource.h>
  39. #include <ctype.h>
  40.  
  41. int original_stack_limit;
  42. #endif
  43.  
  44. /* If this definition isn't overridden by the header files, assume
  45.    that isatty and fileno exist on this system.  */
  46. #ifndef ISATTY
  47. #define ISATTY(FP)    (isatty (fileno (FP)))
  48. #endif
  49.  
  50. extern void free ();
  51.  
  52. /* Version number of GDB, as a string.  */
  53.  
  54. extern char *version;
  55.  
  56. /*
  57.  * Declare all cmd_list_element's
  58.  */
  59.  
  60. /* Chain containing all defined commands.  */
  61.  
  62. struct cmd_list_element *cmdlist;
  63.  
  64. /* Chain containing all defined info subcommands.  */
  65.  
  66. struct cmd_list_element *infolist;
  67.  
  68. /* Chain containing all defined enable subcommands. */
  69.  
  70. struct cmd_list_element *enablelist;
  71.  
  72. /* Chain containing all defined disable subcommands. */
  73.  
  74. struct cmd_list_element *disablelist;
  75.  
  76. /* Chain containing all defined delete subcommands. */
  77.  
  78. struct cmd_list_element *deletelist;
  79.  
  80. /* Chain containing all defined "enable breakpoint" subcommands. */
  81.  
  82. struct cmd_list_element *enablebreaklist;
  83.  
  84. /* Chain containing all defined set subcommands */
  85.  
  86. struct cmd_list_element *setlist;
  87.  
  88. /* Chain containing all defined \"set history\".  */
  89.  
  90. struct cmd_list_element *sethistlist;
  91.  
  92. /* Chain containing all defined \"unset history\".  */
  93.  
  94. struct cmd_list_element *unsethistlist;
  95.  
  96. /* stdio stream that command input is being read from.  */
  97.  
  98. FILE *instream;
  99.  
  100. /* Current working directory.  */
  101.  
  102. char *current_directory;
  103.  
  104. /* The directory name is actually stored here (usually).  */
  105. static char dirbuf[MAXPATHLEN];
  106.  
  107. /* The number of lines on a page, and the number of spaces
  108.    in a line.  */
  109. int linesize, pagesize;
  110.  
  111. /* Nonzero if we should refrain from using an X window.  */
  112.  
  113. int inhibit_windows = 0;
  114.  
  115. /* Function to call before reading a command, if nonzero.
  116.    The function receives two args: an input stream,
  117.    and a prompt string.  */
  118.    
  119. void (*window_hook) ();
  120.  
  121. extern int frame_file_full_name;
  122. int xgdb_verbose;
  123.  
  124. void free_command_lines ();
  125. char *gdb_readline ();
  126. char *command_line_input ();
  127. static void initialize_main ();
  128. static void initialize_cmd_lists ();
  129. void command_loop ();
  130. static void source_command ();
  131. static void print_gdb_version ();
  132. static void float_handler ();
  133. static void cd_command ();
  134.  
  135. char *getenv ();
  136.  
  137. /* gdb prints this when reading a command interactively */
  138. static char *prompt;
  139.  
  140. /* Buffer used for reading command lines, and the size
  141.    allocated for it so far.  */
  142.  
  143. char *line;
  144. int linesize;
  145.  
  146.  
  147. /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT.  */
  148.  
  149. #ifndef STOP_SIGNAL
  150. #ifdef SIGTSTP
  151. #define STOP_SIGNAL SIGTSTP
  152. #endif
  153. #endif
  154.  
  155. /* This is how `error' returns to command level.  */
  156.  
  157. jmp_buf to_top_level;
  158.  
  159. void
  160. return_to_top_level ()
  161. {
  162.   quit_flag = 0;
  163.   immediate_quit = 0;
  164.   clear_breakpoint_commands ();
  165.   clear_momentary_breakpoints ();
  166.   disable_current_display ();
  167.   do_cleanups (0);
  168.   longjmp (to_top_level, 1);
  169. }
  170.  
  171. /* Call FUNC with arg ARG, catching any errors.
  172.    If there is no error, return the value returned by FUNC.
  173.    If there is an error, return zero after printing ERRSTRING
  174.     (which is in addition to the specific error message already printed).  */
  175.  
  176. int
  177. catch_errors (func, arg, errstring)
  178.      int (*func) ();
  179.      int arg;
  180.      char *errstring;
  181. {
  182.   jmp_buf saved;
  183.   int val;
  184.   struct cleanup *saved_cleanup_chain;
  185.  
  186.   saved_cleanup_chain = save_cleanups ();
  187.  
  188.   bcopy (to_top_level, saved, sizeof (jmp_buf));
  189.  
  190.   if (setjmp (to_top_level) == 0)
  191.     val = (*func) (arg);
  192.   else
  193.     {
  194.       fprintf (stderr, "%s\n", errstring);
  195.       val = 0;
  196.     }
  197.  
  198.   restore_cleanups (saved_cleanup_chain);
  199.  
  200.   bcopy (saved, to_top_level, sizeof (jmp_buf));
  201.   return val;
  202. }
  203.  
  204. /* Handler for SIGHUP.  */
  205.  
  206. static void
  207. disconnect ()
  208. {
  209.   kill_inferior_fast ();
  210.   signal (SIGHUP, SIG_DFL);
  211.   kill (getpid (), SIGHUP);
  212. }
  213.  
  214. /* Clean up on error during a "source" command (or execution of a
  215.    user-defined command).
  216.    Close the file opened by the command
  217.    and restore the previous input stream.  */
  218.  
  219. static void
  220. source_cleanup (stream)
  221.      FILE *stream;
  222. {
  223.   /* Instream may be 0; set to it when executing user-defined command. */
  224.   if (instream)
  225.     fclose (instream);
  226.   instream = stream;
  227. }
  228.  
  229.  
  230. int
  231. main (argc, argv, envp)
  232.      int argc;
  233.      char **argv;
  234.      char **envp;
  235. {
  236.   int count;
  237.   int inhibit_gdbinit = 0;
  238.   int quiet = 0;
  239.   int batch = 0;
  240.   register int i;
  241.  
  242. #if defined (ALIGN_STACK_ON_STARTUP)
  243.   i = (int) &count & 0x3;
  244.   if (i != 0)
  245.     alloca (4 - i);
  246. #endif
  247.  
  248.   quit_flag = 0;
  249.   linesize = 100;
  250.   line = (char *) xmalloc (linesize);
  251.   instream = stdin;
  252.  
  253.   getwd (dirbuf);
  254.   current_directory = dirbuf;
  255.  
  256. #ifdef SET_STACK_LIMIT_HUGE
  257.   {
  258.     struct rlimit rlim;
  259.  
  260.     /* Set the stack limit huge so that alloca (particularly stringtab
  261.      * in dbxread.c) does not fail. */
  262.     getrlimit (RLIMIT_STACK, &rlim);
  263.     original_stack_limit = rlim.rlim_cur;
  264.     rlim.rlim_cur = rlim.rlim_max;
  265.     setrlimit (RLIMIT_STACK, &rlim);
  266.   }
  267. #endif /* SET_STACK_LIMIT_HUGE */
  268.  
  269.   /* Look for flag arguments.  */
  270.  
  271.   for (i = 1; i < argc; i++)
  272.     {
  273.       if (!strcmp (argv[i], "-q") || !strcmp (argv[i], "-quiet"))
  274.     quiet = 1;
  275.       else if (!strcmp (argv[i], "-nx"))
  276.     inhibit_gdbinit = 1;
  277.       else if (!strcmp (argv[i], "-nw"))
  278.     inhibit_windows = 1;
  279.       else if (!strcmp (argv[i], "-batch"))
  280.     batch = 1, quiet = 1;
  281.       else if (!strcmp (argv[i], "-fullname"))
  282.     frame_file_full_name = 1;
  283.       else if (!strcmp (argv[i], "-xgdb_verbose"))
  284.     xgdb_verbose = 1;
  285.       /* -help: print a summary of command line switches.  */
  286.       else if (!strcmp (argv[i], "-help"))
  287.     {
  288.       fputs ("\
  289. This is GDB, the GNU debugger.  Use the command\n\
  290.     gdb [options] [executable [core-file]]\n\
  291. to enter the debugger.\n\
  292. \n\
  293. Options available are:\n\
  294.   -help             Print this message.\n\
  295.   -quiet            Do not print version number on startup.\n\
  296.   -fullname         Output information used by emacs-GDB interface.\n\
  297.   -batch            Exit after processing options.\n\
  298.   -nx               Do not read .gdbinit file.\n\
  299.   -tty TTY          Use TTY for input/output by the program being debugged.\n\
  300.   -cd DIR           Change current directory to DIR.\n\
  301.   -directory DIR    Search for source files in DIR.\n\
  302.   -command FILE     Execute GDB commands from FILE.\n\
  303.   -symbols SYMFILE  Read symbols from SYMFILE.\n\
  304.   -exec EXECFILE    Use EXECFILE as the executable.\n\
  305.   -se FILE          Use FILE as symbol file and executable file.\n\
  306.   -core COREFILE    Analyze the core dump COREFILE.\n\
  307. \n\
  308. For more information, type \"help\" from within GDB, or consult the\n\
  309. GDB manual (available as on-line info or a printed manual).\n", stderr);
  310.       /* Exiting after printing this message seems like
  311.          the most useful thing to do.  */
  312.       exit (0);
  313.     }
  314.       else if (argv[i][0] == '-')
  315.     /* Other options take arguments, so don't confuse an
  316.        argument with an option.  */
  317.     i++;
  318.     }
  319.  
  320.   /* Run the init function of each source file */
  321.  
  322.   initialize_cmd_lists ();    /* This needs to be done first */
  323.   initialize_all_files ();
  324.   initialize_main ();        /* But that omits this file!  Do it now */
  325.   initialize_signals ();
  326.  
  327.   if (!quiet)
  328.     print_gdb_version ();
  329.  
  330.   /* Process the command line arguments.  */
  331.  
  332.   count = 0;
  333.   for (i = 1; i < argc; i++)
  334.     {
  335.       register char *arg = argv[i];
  336.       /* Args starting with - say what to do with the following arg
  337.      as a filename.  */
  338.       if (arg[0] == '-')
  339.     {
  340.       extern void exec_file_command (), symbol_file_command ();
  341.       extern void core_file_command (), directory_command ();
  342.       extern void tty_command ();
  343.  
  344.       if (!strcmp (arg, "-q") || !strcmp (arg, "-nx")
  345.           || !strcmp (arg, "-quiet") || !strcmp (arg, "-batch")
  346.           || !strcmp (arg, "-fullname") || !strcmp (arg, "-nw")
  347.           || !strcmp (arg, "-xgdb_verbose")
  348.           || !strcmp (arg, "-help"))
  349.         /* Already processed above */
  350.         continue;
  351.  
  352.       if (++i == argc)
  353.         fprintf (stderr, "No argument follows \"%s\".\n", arg);
  354.       if (!setjmp (to_top_level))
  355.         {
  356.           /* -s foo: get syms from foo.  -e foo: execute foo.
  357.          -se foo: do both with foo.  -c foo: use foo as core dump.  */
  358.           if (!strcmp (arg, "-se"))
  359.         {
  360.           exec_file_command (argv[i], !batch);
  361.           symbol_file_command (argv[i], !batch);
  362.         }
  363.           else if (!strcmp (arg, "-s") || !strcmp (arg, "-symbols"))
  364.         symbol_file_command (argv[i], !batch);
  365.           else if (!strcmp (arg, "-e") || !strcmp (arg, "-exec"))
  366.         exec_file_command (argv[i], !batch);
  367.           else if (!strcmp (arg, "-c") || !strcmp (arg, "-core"))
  368. #if (defined(sprite) && defined(mips))
  369.         error("\"%s\" option not supported on mips yet\n", arg);
  370. #else
  371.         core_file_command (argv[i], !batch);
  372. #endif
  373.           /* -x foo: execute commands from foo.  */
  374.           else if (!strcmp (arg, "-x") || !strcmp (arg, "-command")
  375.                || !strcmp (arg, "-commands"))
  376.         source_command (argv[i]);
  377.           /* -d foo: add directory `foo' to source-file directory
  378.                  search-list */
  379.           else if (!strcmp (arg, "-d") || !strcmp (arg, "-dir")
  380.                || !strcmp (arg, "-directory"))
  381.         directory_command (argv[i], 0);
  382.           /* -cd FOO: specify current directory as FOO.
  383.          GDB remembers the precise string FOO as the dirname.  */
  384.           else if (!strcmp (arg, "-cd"))
  385.         {
  386.           cd_command (argv[i], 0);
  387.           init_source_path ();
  388.         }
  389.           /* -t /def/ttyp1: use /dev/ttyp1 for inferior I/O.  */
  390.           else if (!strcmp (arg, "-t") || !strcmp (arg, "-tty"))
  391.         tty_command (argv[i], 0);
  392.  
  393.           else
  394.         error ("Unknown command-line switch: \"%s\"\n", arg);
  395.         }
  396.     }
  397.       else
  398.     {
  399.       /* Args not thus accounted for
  400.          are treated as, first, the symbol/executable file
  401.          and, second, the core dump file.  */
  402.       count++;
  403.       if (!setjmp (to_top_level))
  404.         switch (count)
  405.           {
  406.           case 1:
  407.         exec_file_command (arg, !batch);
  408.         symbol_file_command (arg, !batch);
  409.         break;
  410.  
  411.           case 2:
  412. #if (defined(sprite) && defined(mips))
  413.         error("Core files not supported on mips yet\n");
  414. #else
  415.         core_file_command (arg, !batch);
  416. #endif
  417.         break;
  418.  
  419.           case 3:
  420.         fprintf (stderr, "Excess command line args ignored. (%s%s)\n",
  421.              arg, (i == argc - 1) ? "" : " ...");
  422.           }
  423.     }
  424.     }
  425.  
  426.   {
  427.     struct stat homebuf, cwdbuf;
  428.     char *homedir, *homeinit;
  429.  
  430.     /* Read init file, if it exists in home directory  */
  431.     homedir = getenv ("HOME");
  432.     if (homedir)
  433.       {
  434.     homeinit = (char *) alloca (strlen (getenv ("HOME")) + 10);
  435.     strcpy (homeinit, getenv ("HOME"));
  436.     strcat (homeinit, "/.gdbinit");
  437.     if (!inhibit_gdbinit && access (homeinit, R_OK) == 0)
  438.       if (!setjmp (to_top_level))
  439.         source_command (homeinit);
  440.  
  441.     /* Do stats; no need to do them elsewhere since we'll only
  442.        need them if homedir is set.  Make sure that they are
  443.        zero in case one of them fails (guarantees that they
  444.        won't match if either exits).  */
  445.     
  446.     bzero (&homebuf, sizeof (struct stat));
  447.     bzero (&cwdbuf, sizeof (struct stat));
  448.     
  449.     stat (homeinit, &homebuf);
  450.     stat ("./.gdbinit", &cwdbuf); /* We'll only need this if
  451.                      homedir was set.  */
  452.       }
  453.     
  454.     /* Read the input file in the current directory, *if* it isn't
  455.        the same file (it should exist, also).  */
  456.  
  457.     if (!homedir
  458.     || bcmp ((char *) &homebuf,
  459.          (char *) &cwdbuf,
  460.          sizeof (struct stat)))
  461.       if (!inhibit_gdbinit && access (".gdbinit", R_OK) == 0)
  462.     if (!setjmp (to_top_level))
  463.       source_command (".gdbinit");
  464.   }
  465.  
  466.   if (batch)
  467.     {
  468. #if 0
  469.       fatal ("Attempt to read commands from stdin in batch mode.");
  470. #endif
  471.       /* We have hit the end of the batch file.  */
  472.       exit (0);
  473.     }
  474.  
  475.   if (!quiet)
  476.     printf ("Type \"help\" for a list of commands.\n");
  477.  
  478.   /* The command loop.  */
  479.  
  480.   while (1)
  481.     {
  482.       if (!setjmp (to_top_level))
  483.     command_loop ();
  484.       clearerr (stdin);        /* Don't get hung if C-d is typed.  */
  485.     }
  486. }
  487.  
  488. /* Execute the line P as a command.
  489.    Pass FROM_TTY as second argument to the defining function.  */
  490.  
  491. void
  492. execute_command (p, from_tty)
  493.      char *p;
  494.      int from_tty;
  495. {
  496.   register struct cmd_list_element *c;
  497.   register struct command_line *cmdlines;
  498.  
  499.   free_all_values ();
  500.   while (*p == ' ' || *p == '\t') p++;
  501.   if (*p)
  502.     {
  503.       c = lookup_cmd (&p, cmdlist, "", 0, 1);
  504.       if (c->function == 0)
  505.     error ("That is not a command, just a help topic.");
  506.       else if (c->class == (int) class_user)
  507.     {
  508.       struct cleanup *old_chain;
  509.       
  510.       if (*p)
  511.         error ("User-defined commands cannot take arguments.");
  512.       cmdlines = (struct command_line *) c->function;
  513.       if (cmdlines == (struct command_line *) 0)
  514.         /* Null command */
  515.         return;
  516.  
  517.       /* Set the instream to 0, indicating execution of a
  518.          user-defined function.  */
  519.       old_chain =  make_cleanup (source_cleanup, instream);
  520.       instream = (FILE *) 0;
  521.       while (cmdlines)
  522.         {
  523.           execute_command (cmdlines->line, 0);
  524.           cmdlines = cmdlines->next;
  525.         }
  526.       do_cleanups (old_chain);
  527.     }
  528.       else
  529.     /* Pass null arg rather than an empty one.  */
  530.     (*c->function) (*p ? p : 0, from_tty);
  531.     }
  532. }
  533.  
  534. static void
  535. do_nothing ()
  536. {
  537. }
  538.  
  539. /* Read commands from `instream' and execute them
  540.    until end of file.  */
  541. void
  542. command_loop ()
  543. {
  544.   struct cleanup *old_chain;
  545.   while (!feof (instream))
  546.     {
  547.       if (window_hook && instream == stdin)
  548.     (*window_hook) (instream, prompt);
  549.  
  550.       quit_flag = 0;
  551.       if (instream == stdin && ISATTY (stdin))
  552.     reinitialize_more_filter ();
  553.       old_chain = make_cleanup (do_nothing, 0);
  554.       execute_command (command_line_input (instream == stdin ? prompt : 0,
  555.                       instream == stdin),
  556.                instream == stdin);
  557.       /* Do any commands attached to breakpoint we stopped at.  */
  558.       do_breakpoint_commands ();
  559.       do_cleanups (old_chain);
  560.     }
  561. }
  562.  
  563. /* Commands call this if they do not want to be repeated by null lines.  */
  564.  
  565. void
  566. dont_repeat ()
  567. {
  568.   /* If we aren't reading from standard input, we are saving the last
  569.      thing read from stdin in line and don't want to delete it.  Null lines
  570.      won't repeat here in any case.  */
  571.   if (instream == stdin)
  572.     *line = 0;
  573. }
  574.  
  575. /* Read a line from the stream "instream" without command line editing.
  576.  
  577.    It prints PROMPT once at the start.
  578.  
  579.    If RETURN_RESULT is set it allocates
  580.    space for whatever the user types and returns the result.
  581.    If not, it just discards what the user types.  */
  582. char *
  583. gdb_readline (prompt, return_result)
  584.      char *prompt;
  585.      int return_result;
  586. {
  587.   int c;
  588.   char *result;
  589.   int input_index = 0;
  590.   int result_size = 80;
  591.  
  592.   if (prompt)
  593.     {
  594.       printf (prompt);
  595.       fflush (stdout);
  596.     }
  597.   
  598.   if (return_result)
  599.     result = (char *) xmalloc (result_size);
  600.  
  601.   while (1)
  602.     {
  603.       c = fgetc (instream);
  604.       if (c == -1 || c == '\n')
  605.     break;
  606.       if (return_result)
  607.     {
  608.       result[input_index++] = c;
  609.       while (input_index >= result_size)
  610.         {
  611.           result_size *= 2;
  612.           result = (char *) xrealloc (result, result_size);
  613.         }
  614.     }
  615.     }
  616.   if (return_result)
  617.     {
  618.       result[input_index++] = '\0';
  619.       return result;
  620.     }
  621.   else
  622.     return (char *) 0;
  623. }
  624.  
  625. /* Declaration for fancy readline with command line editing.  */
  626. char *readline ();
  627.  
  628. /* Variables which control command line editing and history
  629.    substitution.  These variables are given default values at the end
  630.    of this file.  */
  631. static int command_editing_p;
  632. static int history_expansion_p;
  633. static int write_history_p;
  634. static int history_size;
  635. static char *history_filename;
  636.  
  637. /* Variables which are necessary for fancy command line editing.  */
  638. char *gdb_completer_word_break_characters =
  639.   " \t\n!@#$%^&*()-+=|~`}{[]\"';:?/>.<,";
  640.  
  641. /* Functions that are used as part of the fancy command line editing.  */
  642.  
  643. /* Generate symbol names one by one for the completer.  If STATE is
  644.    zero, then we need to initialize, otherwise the initialization has
  645.    already taken place.  TEXT is what we expect the symbol to start
  646.    with.  RL_LINE_BUFFER is available to be looked at; it contains the
  647.    entire text of the line.  RL_POINT is the offset in that line of
  648.    the cursor.  You should pretend that the line ends at RL_POINT.  */
  649. char *
  650. symbol_completion_function (text, state)
  651.      char *text;
  652.      int state;
  653. {
  654.   char **make_symbol_completion_list ();
  655.   static char **list = (char **)NULL;
  656.   static int index;
  657.   char *output;
  658.   extern char *rl_line_buffer;
  659.   extern int rl_point;
  660.   char *tmp_command, *p;
  661.   struct cmd_list_element *c, *result_list;
  662.  
  663.   if (!state)
  664.     {
  665.       /* Free the storage used by LIST, but not by the strings inside.  This is
  666.      because rl_complete_internal () frees the strings. */
  667.       if (list)
  668.     free (list);
  669.       list = 0;
  670.       index = 0;
  671.  
  672.       /* Decide whether to complete on a list of gdb commands or on
  673.      symbols.  */
  674.       tmp_command = (char *) alloca (rl_point + 1);
  675.       p = tmp_command;
  676.       
  677.       strncpy (tmp_command, rl_line_buffer, rl_point);
  678.       tmp_command[rl_point] = '\0';
  679.  
  680.       if (rl_point == 0)
  681.     {
  682.       /* An empty line we want to consider ambiguous; that is,
  683.          it could be any command.  */
  684.       c = (struct cmd_list_element *) -1;
  685.       result_list = 0;
  686.     }
  687.       else
  688.     c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
  689.  
  690.       /* Move p up to the next interesting thing.  */
  691.       while (*p == ' ' || *p == '\t')
  692.     p++;
  693.  
  694.       if (!c)
  695.     /* He's typed something unrecognizable.  Sigh.  */
  696.     list = (char **) 0;
  697.       else if (c == (struct cmd_list_element *) -1)
  698.     {
  699.       if (p + strlen(text) != tmp_command + rl_point)
  700.         error ("Unrecognized command.");
  701.       
  702.       /* He's typed something ambiguous.  This is easier.  */
  703.       if (result_list)
  704.         list = complete_on_cmdlist (*result_list->prefixlist, text);
  705.       else
  706.         list = complete_on_cmdlist (cmdlist, text);
  707.     }
  708.       else
  709.     {
  710.       /* If we've gotten this far, gdb has recognized a full
  711.          command.  There are several possibilities:
  712.  
  713.          1) We need to complete on the command.
  714.          2) We need to complete on the possibilities coming after
  715.          the command.
  716.          2) We need to complete the text of what comes after the
  717.          command.   */
  718.  
  719.       if (!*p && *text)
  720.         /* Always (might be longer versions of thie command).  */
  721.         list = complete_on_cmdlist (result_list, text);
  722.       else if (!*p && !*text)
  723.         {
  724.           if (c->prefixlist)
  725.         list = complete_on_cmdlist (*c->prefixlist, "");
  726.           else
  727.         list = make_symbol_completion_list ("");
  728.         }
  729.       else
  730.         {
  731.           if (c->prefixlist && !c->allow_unknown)
  732.         {
  733.           *p = '\0';
  734.           error ("\"%s\" command requires a subcommand.",
  735.              tmp_command);
  736.         }
  737.           else
  738.         list = make_symbol_completion_list (text);
  739.         }
  740.     }
  741.     }
  742.  
  743.   /* If the debugged program wasn't compiled with symbols, or if we're
  744.      clearly completing on a command and no command matches, return
  745.      NULL.  */
  746.   if (!list)
  747.     return ((char *)NULL);
  748.  
  749.   output = list[index];
  750.   if (output)
  751.     index++;
  752.  
  753.   return (output);
  754. }
  755.  
  756. #ifdef STOP_SIGNAL
  757. static void
  758. stop_sig ()
  759. {
  760. #if STOP_SIGNAL == SIGTSTP
  761.   signal (SIGTSTP, SIG_DFL);
  762.   sigsetmask (0);
  763.   kill (getpid (), SIGTSTP);
  764.   signal (SIGTSTP, stop_sig);
  765. #else
  766.   signal (STOP_SIGNAL, stop_sig);
  767. #endif
  768.   printf ("%s", prompt);
  769.   fflush (stdout);
  770.  
  771.   /* Forget about any previous command -- null line now will do nothing.  */
  772.   dont_repeat ();
  773. }
  774. #endif /* STOP_SIGNAL */
  775.  
  776. #if 0
  777. Writing the history file upon a terminating signal is not useful,
  778.   because the info is rarely relevant and is in the core dump anyway.
  779.   It is an annoyance to have the file cluttering up the place.
  780. /* The list of signals that would terminate us if not caught.
  781.    We catch them, but just so that we can write the history file,
  782.    and so forth. */
  783. int terminating_signals[] = {
  784.   SIGHUP, SIGINT, SIGILL, SIGTRAP, SIGIOT,
  785.   SIGEMT, SIGFPE, SIGKILL, SIGBUS, SIGSEGV, SIGSYS,
  786.   SIGPIPE, SIGALRM, SIGTERM,
  787. #ifdef SIGXCPU
  788.   SIGXCPU,
  789. #endif
  790. #ifdef SIGXFSZ
  791.   SIGXFSZ,
  792. #endif
  793. #ifdef SIGVTALRM
  794.   SIGVTALRM,
  795. #endif
  796. #ifdef SIGPROF
  797.   SIGPROF,
  798. #endif
  799. #ifdef SIGLOST
  800.   SIGLOST,
  801. #endif
  802. #ifdef SIGUSR1
  803.   SIGUSR1, SIGUSR2
  804. #endif
  805.     };
  806.  
  807. #define TERMSIGS_LENGTH (sizeof (terminating_signals) / sizeof (int))
  808.  
  809. static void
  810. catch_termination (sig)
  811.      int sig;
  812. {
  813.   /* We are probably here because GDB has a bug.  Write out the history
  814.      so that we might have a better chance of reproducing it.  */
  815.   /* Tell the user what we are doing so he can delete the file if
  816.      it is unwanted.  */
  817.   write_history (history_filename);
  818.   printf ("\n%s written.\n", history_filename);
  819.   signal (sig, SIG_DFL);
  820.   kill (getpid (), sig);
  821. }
  822. #endif
  823.  
  824. /* Initialize signal handlers. */
  825. initialize_signals ()
  826. {
  827.   extern void request_quit ();
  828. #if 0
  829.   register int i;
  830.  
  831.   for (i = 0; i < TERMSIGS_LENGTH; i++)
  832.     signal (terminating_signals[i], catch_termination);
  833. #endif
  834.  
  835.   signal (SIGINT, request_quit);
  836.  
  837.   /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
  838.      passed to the inferior, which we don't want.  It would be
  839.      possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
  840.      on BSD4.3 systems using vfork, that will (apparently) affect the
  841.      GDB process as well as the inferior (the signal handling tables
  842.      being shared between the two, apparently).  Since we establish
  843.      a handler for SIGQUIT, when we call exec it will set the signal
  844.      to SIG_DFL for us.  */
  845.   signal (SIGQUIT, do_nothing);
  846.   if (signal (SIGHUP, do_nothing) != SIG_IGN)
  847.     signal (SIGHUP, disconnect);
  848.   signal (SIGFPE, float_handler);
  849. }
  850.  
  851. /* Read one line from the command input stream `instream'
  852.    into the local static buffer `linebuffer' (whose current length
  853.    is `linelength').
  854.    The buffer is made bigger as necessary.
  855.    Returns the address of the start of the line.
  856.  
  857.    *If* the instream == stdin & stdin is a terminal, the line read
  858.    is copied into the file line saver (global var char *line,
  859.    length linesize) so that it can be duplicated.
  860.  
  861.    This routine either uses fancy command line editing or
  862.    simple input as the user has requested.  */
  863.  
  864. char *
  865. command_line_input (prompt, repeat)
  866.      char *prompt;
  867.      int repeat;
  868. {
  869.   static char *linebuffer = 0;
  870.   static int linelength = 0;
  871.   register char *p;
  872.   register char *p1, *rl;
  873.   char *local_prompt = prompt;
  874.   register int c;
  875.   char *nline;
  876.  
  877.   if (linebuffer == 0)
  878.     {
  879.       linelength = 80;
  880.       linebuffer = (char *) xmalloc (linelength);
  881.     }
  882.  
  883.   p = linebuffer;
  884.  
  885.   /* Control-C quits instantly if typed while in this loop
  886.      since it should not wait until the user types a newline.  */
  887.   immediate_quit++;
  888. #ifdef STOP_SIGNAL
  889.   signal (STOP_SIGNAL, stop_sig);
  890. #endif
  891.  
  892.   while (1)
  893.     {
  894.       /* Don't use fancy stuff if not talking to stdin.  */
  895.       if (command_editing_p && instream == stdin
  896.       && ISATTY (instream))
  897.     rl = readline (local_prompt);
  898.       else
  899.     rl = gdb_readline (local_prompt, 1);
  900.  
  901.       if (!rl || rl == (char *) EOF) break;
  902.       if (strlen(rl) + 1 + (p - linebuffer) > linelength)
  903.     {
  904.       linelength = strlen(rl) + 1 + (p - linebuffer);
  905.       nline = (char *) xrealloc (linebuffer, linelength);
  906.       p += nline - linebuffer;
  907.       linebuffer = nline;
  908.     }
  909.       p1 = rl;
  910.       /* Copy line.  Don't copy null at end.  (Leaves line alone
  911.          if this was just a newline)  */
  912.       while (*p1)
  913.     *p++ = *p1++;
  914.  
  915.       free (rl);            /* Allocated in readline.  */
  916.  
  917.       if (p == linebuffer || *(p - 1) != '\\')
  918.     break;
  919.  
  920.       p--;            /* Put on top of '\'.  */
  921.       local_prompt = (char *) 0;
  922.   }
  923.  
  924. #ifdef STOP_SIGNAL
  925.   signal (SIGTSTP, SIG_DFL);
  926. #endif
  927.   immediate_quit--;
  928.  
  929.   /* Do history expansion if that is wished.  */
  930.   if (history_expansion_p && instream == stdin
  931.       && ISATTY (instream))
  932.     {
  933.       char *history_value;
  934.       int expanded;
  935.  
  936.       *p = '\0';        /* Insert null now.  */
  937.       expanded = history_expand (linebuffer, &history_value);
  938.       if (expanded)
  939.     {
  940.       /* Print the changes.  */
  941.       printf ("%s\n", history_value);
  942.  
  943.       /* If there was an error, call this function again.  */
  944.       if (expanded < 0)
  945.         {
  946.           free (history_value);
  947.           return command_line_input (prompt, repeat);
  948.         }
  949.       if (strlen (history_value) > linelength)
  950.         {
  951.           linelength = strlen (history_value) + 1;
  952.           linebuffer = (char *) xrealloc (linebuffer, linelength);
  953.         }
  954.       strcpy (linebuffer, history_value);
  955.       p = linebuffer + strlen(linebuffer);
  956.       free (history_value);
  957.     }
  958.     }
  959.  
  960.   /* If we just got an empty line, and that is supposed
  961.      to repeat the previous command, return the value in the
  962.      global buffer.  */
  963.   if (repeat)
  964.     {
  965.       if (p == linebuffer)
  966.     return line;
  967.       p1 = linebuffer;
  968.       while (*p1 == ' ' || *p1 == '\t')
  969.     p1++;
  970.       if (!*p1)
  971.     return line;
  972.     }
  973.  
  974.   *p = 0;
  975.  
  976.   /* Add line to history if appropriate.  */
  977.   if (instream == stdin
  978.       && ISATTY (stdin) && *linebuffer)
  979.     add_history (linebuffer);
  980.  
  981.   /* If line is a comment, clear it out.  */
  982.   /* Note:  comments are added to the command history.
  983.      This is useful when you type a command, and then realize
  984.      you don't want to execute it quite yet.  You can comment out the
  985.      command and then later fetch it from the value history and
  986.      remove the '#'.  */
  987.   p1 = linebuffer;
  988.   while ((c = *p1) == ' ' || c == '\t') p1++;
  989.   if (c == '#')
  990.     *linebuffer = 0;
  991.  
  992.   /* Save into global buffer if appropriate.  */
  993.   if (repeat)
  994.     {
  995.       if (linelength > linesize)
  996.     {
  997.       line = xrealloc (line, linelength);
  998.       linesize = linelength;
  999.     }
  1000.       strcpy (line, linebuffer);
  1001.       return line;
  1002.     }
  1003.  
  1004.   return linebuffer;
  1005. }
  1006.  
  1007. /* Read lines from the input stream
  1008.    and accumulate them in a chain of struct command_line's
  1009.    which is then returned.  */
  1010.  
  1011. struct command_line *
  1012. read_command_lines ()
  1013. {
  1014.   struct command_line *first = 0;
  1015.   register struct command_line *next, *tail = 0;
  1016.   register char *p, *p1;
  1017.   struct cleanup *old_chain = 0;
  1018.  
  1019.   while (1)
  1020.     {
  1021.       dont_repeat ();
  1022.       p = command_line_input (0, instream == stdin);
  1023.       /* Remove leading and trailing blanks.  */
  1024.       while (*p == ' ' || *p == '\t') p++;
  1025.       p1 = p + strlen (p);
  1026.       while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t')) p1--;
  1027.  
  1028.       /* Is this "end"?  */
  1029.       if (p1 - p == 3 && !strncmp (p, "end", 3))
  1030.     break;
  1031.  
  1032.       /* No => add this line to the chain of command lines.  */
  1033.       next = (struct command_line *) xmalloc (sizeof (struct command_line));
  1034.       next->line = savestring (p, p1 - p);
  1035.       next->next = 0;
  1036.       if (tail)
  1037.     {
  1038.       tail->next = next;
  1039.     }
  1040.       else
  1041.     {
  1042.       /* We just read the first line.
  1043.          From now on, arrange to throw away the lines we have
  1044.          if we quit or get an error while inside this function.  */
  1045.       first = next;
  1046.       old_chain = make_cleanup (free_command_lines, &first);
  1047.     }
  1048.       tail = next;
  1049.     }
  1050.  
  1051.   dont_repeat ();
  1052.  
  1053.   /* Now we are about to return the chain to our caller,
  1054.      so freeing it becomes his responsibility.  */
  1055.   if (first)
  1056.     discard_cleanups (old_chain);
  1057.   return first;
  1058. }
  1059.  
  1060. /* Free a chain of struct command_line's.  */
  1061.  
  1062. void
  1063. free_command_lines (lptr)
  1064.       struct command_line **lptr;
  1065. {
  1066.   register struct command_line *l = *lptr;
  1067.   register struct command_line *next;
  1068.  
  1069.   while (l)
  1070.     {
  1071.       next = l->next;
  1072.       free (l->line);
  1073.       free (l);
  1074.       l = next;
  1075.     }
  1076. }
  1077.  
  1078. /* Add an element to the list of info subcommands.  */
  1079.  
  1080. void
  1081. add_info (name, fun, doc)
  1082.      char *name;
  1083.      void (*fun) ();
  1084.      char *doc;
  1085. {
  1086.   add_cmd (name, no_class, fun, doc, &infolist);
  1087. }
  1088.  
  1089. /* Add an alias to the list of info subcommands.  */
  1090.  
  1091. void
  1092. add_info_alias (name, oldname, abbrev_flag)
  1093.      char *name;
  1094.      char *oldname;
  1095.      int abbrev_flag;
  1096. {
  1097.   add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
  1098. }
  1099.  
  1100. /* The "info" command is defined as a prefix, with allow_unknown = 0.
  1101.    Therefore, its own definition is called only for "info" with no args.  */
  1102.  
  1103. static void
  1104. info_command ()
  1105. {
  1106.   printf ("\"info\" must be followed by the name of an info command.\n");
  1107.   help_list (infolist, "info ", -1, stdout);
  1108. }
  1109.  
  1110. /* Add an element to the list of commands.  */
  1111.  
  1112. void
  1113. add_com (name, class, fun, doc)
  1114.      char *name;
  1115.      int class;
  1116.      void (*fun) ();
  1117.      char *doc;
  1118. {
  1119.   add_cmd (name, class, fun, doc, &cmdlist);
  1120. }
  1121.  
  1122. /* Add an alias or abbreviation command to the list of commands.  */
  1123.  
  1124. void
  1125. add_com_alias (name, oldname, class, abbrev_flag)
  1126.      char *name;
  1127.      char *oldname;
  1128.      int class;
  1129.      int abbrev_flag;
  1130. {
  1131.   add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
  1132. }
  1133.  
  1134. void
  1135. error_no_arg (why)
  1136.      char *why;
  1137. {
  1138.   error ("Argument required (%s).", why);
  1139. }
  1140.  
  1141. static void
  1142. help_command (command, from_tty)
  1143.      char *command;
  1144.      int from_tty; /* Ignored */
  1145. {
  1146.   help_cmd (command, stdout);
  1147. }
  1148.  
  1149. static void
  1150. validate_comname (comname)
  1151.      char *comname;
  1152. {
  1153.   register char *p;
  1154.  
  1155.   if (comname == 0)
  1156.     error_no_arg ("name of command to define");
  1157.  
  1158.   p = comname;
  1159.   while (*p)
  1160.     {
  1161.       if (!(*p >= 'A' && *p <= 'Z')
  1162.       && !(*p >= 'a' && *p <= 'z')
  1163.       && !(*p >= '0' && *p <= '9')
  1164.       && *p != '-')
  1165.     error ("Junk in argument list: \"%s\"", p);
  1166.       p++;
  1167.     }
  1168. }
  1169.  
  1170. static void
  1171. define_command (comname, from_tty)
  1172.      char *comname;
  1173.      int from_tty;
  1174. {
  1175.   register struct command_line *cmds;
  1176.   register struct cmd_list_element *c;
  1177.   char *tem = comname;
  1178.  
  1179.   validate_comname (comname);
  1180.  
  1181.   c = lookup_cmd (&tem, cmdlist, "", -1, 1);
  1182.   if (c)
  1183.     {
  1184.       if (c->class == (int) class_user || c->class == (int) class_alias)
  1185.     tem = "Redefine command \"%s\"? ";
  1186.       else
  1187.     tem = "Really redefine built-in command \"%s\"? ";
  1188.       if (!query (tem, comname))
  1189.     error ("Command \"%s\" not redefined.", comname);
  1190.     }
  1191.  
  1192.   if (from_tty)
  1193.     {
  1194.       printf ("Type commands for definition of \"%s\".\n\
  1195. End with a line saying just \"end\".\n", comname);
  1196.       fflush (stdout);
  1197.     }
  1198.   comname = savestring (comname, strlen (comname));
  1199.  
  1200.   cmds = read_command_lines ();
  1201.  
  1202.   if (c && c->class == (int) class_user)
  1203.     free_command_lines (&c->function);
  1204.  
  1205.   add_com (comname, class_user, cmds,
  1206.        (c && c->class == (int) class_user)
  1207.        ? c->doc : savestring ("User-defined.", 13));
  1208. }
  1209.  
  1210. static void
  1211. document_command (comname, from_tty)
  1212.      char *comname;
  1213.      int from_tty;
  1214. {
  1215.   struct command_line *doclines;
  1216.   register struct cmd_list_element *c;
  1217.   char *tem = comname;
  1218.  
  1219.   validate_comname (comname);
  1220.  
  1221.   c = lookup_cmd (&tem, cmdlist, "", 0, 1);
  1222.  
  1223.   if (c->class != (int) class_user)
  1224.     error ("Command \"%s\" is built-in.", comname);
  1225.  
  1226.   if (from_tty)
  1227.     printf ("Type documentation for \"%s\".\n\
  1228. End with a line saying just \"end\".\n", comname);
  1229.  
  1230.   doclines = read_command_lines ();
  1231.  
  1232.   if (c->doc) free (c->doc);
  1233.  
  1234.   {
  1235.     register struct command_line *cl1;
  1236.     register int len = 0;
  1237.  
  1238.     for (cl1 = doclines; cl1; cl1 = cl1->next)
  1239.       len += strlen (cl1->line) + 1;
  1240.  
  1241.     c->doc = (char *) xmalloc (len + 1);
  1242.     *c->doc = 0;
  1243.  
  1244.     for (cl1 = doclines; cl1; cl1 = cl1->next)
  1245.       {
  1246.     strcat (c->doc, cl1->line);
  1247.     if (cl1->next)
  1248.       strcat (c->doc, "\n");
  1249.       }
  1250.   }
  1251.  
  1252.   free_command_lines (&doclines);
  1253. }
  1254.  
  1255. static void
  1256. print_gdb_version ()
  1257. {
  1258.   printf ("GDB %s, Copyright (C) 1989 Free Software Foundation, Inc.\n\
  1259. There is ABSOLUTELY NO WARRANTY for GDB; type \"info warranty\" for details.\n\
  1260. GDB is free software and you are welcome to distribute copies of it\n\
  1261.  under certain conditions; type \"info copying\" to see the conditions.\n",
  1262.       version);
  1263. }
  1264.  
  1265. static void
  1266. version_info ()
  1267. {
  1268.   immediate_quit++;
  1269.   print_gdb_version ();
  1270.   immediate_quit--;
  1271. }
  1272.  
  1273. /* xgdb calls this to reprint the usual GDB prompt.  */
  1274.  
  1275. void
  1276. print_prompt ()
  1277. {
  1278.   printf ("%s", prompt);
  1279.   fflush (stdout);
  1280. }
  1281.  
  1282. /* Command to specify a prompt string instead of "(gdb) ".  */
  1283.  
  1284. static void
  1285. set_prompt_command (text)
  1286.      char *text;
  1287. {
  1288.   char *p, *q;
  1289.   register int c;
  1290.   char *new;
  1291.  
  1292.   if (text == 0)
  1293.     error_no_arg ("string to which to set prompt");
  1294.  
  1295.   new = (char *) xmalloc (strlen (text) + 2);
  1296.   p = text; q = new;
  1297.   while (c = *p++)
  1298.     {
  1299.       if (c == '\\')
  1300.     {
  1301.       /* \ at end of argument is used after spaces
  1302.          so they won't be lost.  */
  1303.       if (*p == 0)
  1304.         break;
  1305.       c = parse_escape (&p);
  1306.       if (c == 0)
  1307.         break; /* C loses */
  1308.       else if (c > 0)
  1309.         *q++ = c;
  1310.     }
  1311.       else
  1312.     *q++ = c;
  1313.     }
  1314.   if (*(p - 1) != '\\')
  1315.     *q++ = ' ';
  1316.   *q++ = '\0';
  1317.   new = (char *) xrealloc (new, q - new);
  1318.   free (prompt);
  1319.   prompt = new;
  1320. }
  1321.  
  1322. static void
  1323. quit_command ()
  1324. {
  1325.   if (have_inferior_p ())
  1326.     {
  1327.       if (query ("The program is running.  Quit anyway? "))
  1328.     {
  1329.       /* Prevent any warning message from reopen_exec_file, in case
  1330.          we have a core file that's inconsistent with the exec file.  */
  1331.       exec_file_command (0, 0);
  1332.       kill_inferior ();
  1333.     }
  1334.       else
  1335.     error ("Not confirmed.");
  1336.     }
  1337.   /* Save the history information if it is appropriate to do so.  */
  1338.   if (write_history_p && history_filename)
  1339.     write_history (history_filename);
  1340.   exit (0);
  1341. }
  1342.  
  1343. int
  1344. input_from_terminal_p ()
  1345. {
  1346.   return instream == stdin;
  1347. }
  1348.  
  1349. static void
  1350. pwd_command (arg, from_tty)
  1351.      char *arg;
  1352.      int from_tty;
  1353. {
  1354.   if (arg) error ("The \"pwd\" command does not take an argument: %s", arg);
  1355.   getwd (dirbuf);
  1356.  
  1357.   if (strcmp (dirbuf, current_directory))
  1358.     printf ("Working directory %s\n (canonically %s).\n",
  1359.         current_directory, dirbuf);
  1360.   else
  1361.     printf ("Working directory %s.\n", current_directory);
  1362. }
  1363.  
  1364. static void
  1365. cd_command (dir, from_tty)
  1366.      char *dir;
  1367.      int from_tty;
  1368. {
  1369.   int len;
  1370.   int change;
  1371.  
  1372.   if (dir == 0)
  1373.     error_no_arg ("new working directory");
  1374.  
  1375.   dir = tilde_expand (dir);
  1376.   make_cleanup (free, dir);
  1377.  
  1378.   len = strlen (dir);
  1379.   dir = savestring (dir, len - (len > 1 && dir[len-1] == '/'));
  1380.   if (dir[0] == '/')
  1381.     current_directory = dir;
  1382.   else
  1383.     {
  1384.       current_directory = concat (current_directory, "/", dir);
  1385.       free (dir);
  1386.     }
  1387.  
  1388.   /* Now simplify any occurrences of `.' and `..' in the pathname.  */
  1389.  
  1390.   change = 1;
  1391.   while (change)
  1392.     {
  1393.       char *p;
  1394.       change = 0;
  1395.  
  1396.       for (p = current_directory; *p;)
  1397.     {
  1398.       if (!strncmp (p, "/./", 2)
  1399.           && (p[2] == 0 || p[2] == '/'))
  1400.         strcpy (p, p + 2);
  1401.       else if (!strncmp (p, "/..", 3)
  1402.            && (p[3] == 0 || p[3] == '/')
  1403.            && p != current_directory)
  1404.         {
  1405.           char *q = p;
  1406.           while (q != current_directory && q[-1] != '/') q--;
  1407.           if (q != current_directory)
  1408.         {
  1409.           strcpy (q-1, p+3);
  1410.           p = q-1;
  1411.         }
  1412.         }
  1413.       else p++;
  1414.     }
  1415.     }
  1416.  
  1417.   if (chdir (dir) < 0)
  1418.     perror_with_name (dir);
  1419.  
  1420.   if (from_tty)
  1421.     pwd_command ((char *) 0, 1);
  1422. }
  1423.  
  1424. static void
  1425. source_command (arg, from_tty)
  1426.      char *arg;
  1427.      int from_tty;
  1428. {
  1429.   FILE *stream;
  1430.   struct cleanup *cleanups;
  1431.   char *file = arg;
  1432.  
  1433.   if (file == 0)
  1434.     /* Let source without arguments read .gdbinit.  */
  1435.     file = ".gdbinit";
  1436.  
  1437.   file = tilde_expand (file);
  1438.   make_cleanup (free, file);
  1439.  
  1440.   stream = fopen (file, "r");
  1441.   if (stream == 0)
  1442.     perror_with_name (file);
  1443.  
  1444.   cleanups = make_cleanup (source_cleanup, instream);
  1445.  
  1446.   instream = stream;
  1447.  
  1448.   command_loop ();
  1449.  
  1450.   do_cleanups (cleanups);
  1451. }
  1452.  
  1453. static void
  1454. echo_command (text)
  1455.      char *text;
  1456. {
  1457.   char *p = text;
  1458.   register int c;
  1459.  
  1460.   if (text)
  1461.     while (c = *p++)
  1462.       {
  1463.     if (c == '\\')
  1464.       {
  1465.         /* \ at end of argument is used after spaces
  1466.            so they won't be lost.  */
  1467.         if (*p == 0)
  1468.           return;
  1469.  
  1470.         c = parse_escape (&p);
  1471.         if (c >= 0)
  1472.           fputc (c, stdout);
  1473.       }
  1474.     else
  1475.       fputc (c, stdout);
  1476.       }
  1477. }
  1478.  
  1479. static void
  1480. dump_me_command ()
  1481. {
  1482.   if (query ("Should GDB dump core? "))
  1483.     {
  1484.       signal (SIGQUIT, SIG_DFL);
  1485.       kill (getpid (), SIGQUIT);
  1486.     }
  1487. }
  1488.  
  1489. int
  1490. parse_binary_operation (caller, arg)
  1491.      char *caller, *arg;
  1492. {
  1493.   int length;
  1494.  
  1495.   if (!arg || !*arg)
  1496.     return 1;
  1497.  
  1498.   length = strlen (arg);
  1499.  
  1500.   while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
  1501.     length--;
  1502.  
  1503.   if (!strncmp (arg, "on", length)
  1504.       || !strncmp (arg, "1", length)
  1505.       || !strncmp (arg, "yes", length))
  1506.     return 1;
  1507.   else
  1508.     if (!strncmp (arg, "off", length)
  1509.     || !strncmp (arg, "0", length)
  1510.     || !strncmp (arg, "no", length))
  1511.       return 0;
  1512.     else
  1513.       error ("\"%s\" not given a binary valued argument.", caller);
  1514. }
  1515.  
  1516. /* Functions to manipulate command line editing control variables.  */
  1517.  
  1518. static void
  1519. set_editing (arg, from_tty)
  1520.      char *arg;
  1521.      int from_tty;
  1522. {
  1523.   command_editing_p = parse_binary_operation ("set command-editing", arg);
  1524. }
  1525.  
  1526. /* Number of commands to print in each call to editing_info.  */
  1527. #define Hist_print 10
  1528. static void
  1529. editing_info (arg, from_tty)
  1530.      char *arg;
  1531.      int from_tty;
  1532. {
  1533.   /* Index for history commands.  Relative to history_base.  */
  1534.   int offset;
  1535.  
  1536.   /* Number of the history entry which we are planning to display next.
  1537.      Relative to history_base.  */
  1538.   static int num = 0;
  1539.  
  1540.   /* The first command in the history which doesn't exist (i.e. one more
  1541.      than the number of the last command).  Relative to history_base.  */
  1542.   int hist_len;
  1543.  
  1544.   struct _hist_entry {
  1545.     char *line;
  1546.     char *data;
  1547.   } *history_get();
  1548.   extern int history_base;
  1549.  
  1550.   printf_filtered ("Interactive command editing is %s.\n",
  1551.       command_editing_p ? "on" : "off");
  1552.  
  1553.   printf_filtered ("History expansion of command input is %s.\n",
  1554.       history_expansion_p ? "on" : "off");
  1555.   printf_filtered ("Writing of a history record upon exit is %s.\n",
  1556.       write_history_p ? "enabled" : "disabled");
  1557.   printf_filtered ("The size of the history list (number of stored commands) is %d.\n",
  1558.       history_size);
  1559.   printf_filtered ("The name of the history record is \"%s\".\n\n",
  1560.       history_filename ? history_filename : "");
  1561.  
  1562.   /* Print out some of the commands from the command history.  */
  1563.   /* First determine the length of the history list.  */
  1564.   hist_len = history_size;
  1565.   for (offset = 0; offset < history_size; offset++)
  1566.     {
  1567.       if (!history_get (history_base + offset))
  1568.     {
  1569.       hist_len = offset;
  1570.       break;
  1571.     }
  1572.     }
  1573.  
  1574.   if (arg)
  1575.     {
  1576.       if (arg[0] == '+' && arg[1] == '\0')
  1577.     /* "info editing +" should print from the stored position.  */
  1578.     ;
  1579.       else
  1580.     /* "info editing <exp>" should print around command number <exp>.  */
  1581.     num = (parse_and_eval_address (arg) - history_base) - Hist_print / 2;
  1582.     }
  1583.   /* "info editing" means print the last Hist_print commands.  */
  1584.   else
  1585.     {
  1586.       num = hist_len - Hist_print;
  1587.     }
  1588.  
  1589.   if (num < 0)
  1590.     num = 0;
  1591.  
  1592.   /* If there are at least Hist_print commands, we want to display the last
  1593.      Hist_print rather than, say, the last 6.  */
  1594.   if (hist_len - num < Hist_print)
  1595.     {
  1596.       num = hist_len - Hist_print;
  1597.       if (num < 0)
  1598.     num = 0;
  1599.     }
  1600.  
  1601.   if (num == hist_len - Hist_print)
  1602.     printf_filtered ("The list of the last %d commands is:\n\n", Hist_print);
  1603.   else
  1604.     printf_filtered ("Some of the stored commands are:\n\n");
  1605.  
  1606.   for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
  1607.     {
  1608.       printf_filtered ("%5d  %s\n", history_base + offset,
  1609.           (history_get (history_base + offset))->line);
  1610.     }
  1611.  
  1612.   /* The next command we want to display is the next one that we haven't
  1613.      displayed yet.  */
  1614.   num += Hist_print;
  1615.   
  1616.   /* If the user repeats this command with return, it should do what
  1617.      "info editing +" does.  This is unnecessary if arg is null,
  1618.      because "info editing +" is not useful after "info editing".  */
  1619.   if (from_tty && arg)
  1620.     {
  1621.       arg[0] = '+';
  1622.       arg[1] = '\0';
  1623.     }
  1624. }
  1625.  
  1626. static void
  1627. set_history_expansion (arg, from_tty)
  1628.      char *arg;
  1629.      int from_tty;
  1630. {
  1631.   history_expansion_p = parse_binary_operation ("set history expansion", arg);
  1632. }
  1633.  
  1634. static void
  1635. set_history_write (arg, from_tty)
  1636.      char *arg;
  1637.      int from_tty;
  1638. {
  1639.   write_history_p = parse_binary_operation ("set history write", arg);
  1640. }
  1641.  
  1642. static void
  1643. set_history (arg, from_tty)
  1644.      char *arg;
  1645.      int from_tty;
  1646. {
  1647.   printf ("\"set history\" must be followed by the name of a history subcommand.\n");
  1648.   help_list (sethistlist, "set history ", -1, stdout);
  1649. }
  1650.  
  1651. static void
  1652. set_history_size (arg, from_tty)
  1653.      char *arg;
  1654.      int from_tty;
  1655. {
  1656.   if (!*arg)
  1657.     error_no_arg ("set history size");
  1658.  
  1659.   history_size = atoi (arg);
  1660. }
  1661.  
  1662. static void
  1663. set_history_filename (arg, from_tty)
  1664.      char *arg;
  1665.      int from_tty;
  1666. {
  1667.   int i;
  1668.  
  1669.   if (!arg)
  1670.     error_no_arg ("history file name");
  1671.   
  1672.   arg = tilde_expand (arg);
  1673.   make_cleanup (free, arg);
  1674.  
  1675.   i = strlen (arg) - 1;
  1676.   
  1677.   free (history_filename);
  1678.   
  1679.   while (i > 0 && (arg[i] == ' ' || arg[i] == '\t'))
  1680.     i--;
  1681.  
  1682.   if (!*arg)
  1683.     history_filename = (char *) 0;
  1684.   else
  1685.     history_filename = savestring (arg, i + 1);
  1686.   history_filename[i] = '\0';
  1687. }
  1688.  
  1689. int info_verbose;
  1690.  
  1691. static void
  1692. set_verbose_command (arg, from_tty)
  1693.      char *arg;
  1694.      int from_tty;
  1695. {
  1696.   info_verbose = parse_binary_operation ("set verbose", arg);
  1697. }
  1698.  
  1699. static void
  1700. verbose_info (arg, from_tty)
  1701.      char *arg;
  1702.      int from_tty;
  1703. {
  1704.   if (arg)
  1705.     error ("\"info verbose\" does not take any arguments.\n");
  1706.   
  1707.   printf ("Verbose printing of information is %s.\n",
  1708.       info_verbose ? "on" : "off");
  1709. }
  1710.  
  1711. static void
  1712. float_handler ()
  1713. {
  1714.   error ("Invalid floating value encountered or computed.");
  1715. }
  1716.  
  1717.  
  1718. static void
  1719. initialize_cmd_lists ()
  1720. {
  1721.   cmdlist = (struct cmd_list_element *) 0;
  1722.   infolist = (struct cmd_list_element *) 0;
  1723.   enablelist = (struct cmd_list_element *) 0;
  1724.   disablelist = (struct cmd_list_element *) 0;
  1725.   deletelist = (struct cmd_list_element *) 0;
  1726.   enablebreaklist = (struct cmd_list_element *) 0;
  1727.   setlist = (struct cmd_list_element *) 0;
  1728.   sethistlist = (struct cmd_list_element *) 0;
  1729.   unsethistlist = (struct cmd_list_element *) 0;
  1730. }
  1731.  
  1732. static void
  1733. initialize_main ()
  1734. {
  1735.   char *tmpenv;
  1736.   /* Command line editing externals.  */
  1737.   extern int (*rl_completion_entry_function)();
  1738.   extern char *rl_completer_word_break_characters;
  1739.  
  1740.   /* Set default verbose mode on.  */
  1741.   info_verbose = 1;
  1742.  
  1743. #ifndef KGDB
  1744.   prompt = savestring ("(gdb) ", 6);
  1745. #else
  1746.   prompt = savestring ("(kgdb) ", 7);
  1747. #endif
  1748.   /* Set the important stuff up for command editing.  */
  1749.   command_editing_p = 1;
  1750.   history_expansion_p = 0;
  1751.   write_history_p = 0;
  1752.   
  1753.   if (tmpenv = getenv ("HISTSIZE"))
  1754.     history_size = atoi (tmpenv);
  1755.   else
  1756.     history_size = 256;
  1757.  
  1758.   stifle_history (history_size);
  1759.  
  1760.   if (tmpenv = getenv ("GDBHISTFILE"))
  1761.     history_filename = savestring (tmpenv, strlen(tmpenv));
  1762.   else
  1763.     /* We include the current directory so that if the user changes
  1764.        directories the file written will be the same as the one
  1765.        that was read.  */
  1766.     history_filename = concat (current_directory, "/.gdb_history", "");
  1767.  
  1768.   read_history (history_filename);
  1769.  
  1770.   /* Setup important stuff for command line editing.  */
  1771.   rl_completion_entry_function = (int (*)()) symbol_completion_function;
  1772.   rl_completer_word_break_characters = gdb_completer_word_break_characters;
  1773.  
  1774.   /* Define the classes of commands.
  1775.      They will appear in the help list in the reverse of this order.  */
  1776.  
  1777.   add_cmd ("obscure", class_obscure, 0, "Obscure features.", &cmdlist);
  1778.   add_cmd ("alias", class_alias, 0, "Aliases of other commands.", &cmdlist);
  1779.   add_cmd ("user", class_user, 0, "User-defined commands.\n\
  1780. The commands in this class are those defined by the user.\n\
  1781. Use the \"define\" command to define a command.", &cmdlist);
  1782.   add_cmd ("support", class_support, 0, "Support facilities.", &cmdlist);
  1783.   add_cmd ("status", class_info, 0, "Status inquiries.", &cmdlist);
  1784.   add_cmd ("files", class_files, 0, "Specifying and examining files.", &cmdlist);
  1785.   add_cmd ("breakpoints", class_breakpoint, 0, "Making program stop at certain points.", &cmdlist);
  1786.   add_cmd ("data", class_vars, 0, "Examining data.", &cmdlist);
  1787.   add_cmd ("stack", class_stack, 0, "Examining the stack.\n\
  1788. The stack is made up of stack frames.  Gdb assigns numbers to stack frames\n\
  1789. counting from zero for the innermost (currently executing) frame.\n\n\
  1790. At any time gdb identifies one frame as the \"selected\" frame.\n\
  1791. Variable lookups are done with respect to the selected frame.\n\
  1792. When the program being debugged stops, gdb selects the innermost frame.\n\
  1793. The commands below can be used to select other frames by number or address.",
  1794.        &cmdlist);
  1795.   add_cmd ("running", class_run, 0, "Running the program.", &cmdlist);
  1796.  
  1797.   add_com ("pwd", class_files, pwd_command,
  1798.        "Print working directory.  This is used for your program as well.");
  1799.   add_com ("cd", class_files, cd_command,
  1800.        "Set working directory to DIR for debugger and program being debugged.\n\
  1801. The change does not take effect for the program being debugged\n\
  1802. until the next time it is started.");
  1803.  
  1804.   add_cmd ("prompt", class_support, set_prompt_command,
  1805.        "Change gdb's prompt from the default of \"(gdb)\"",
  1806.        &setlist);
  1807.   add_com ("echo", class_support, echo_command,
  1808.        "Print a constant string.  Give string as argument.\n\
  1809. C escape sequences may be used in the argument.\n\
  1810. No newline is added at the end of the argument;\n\
  1811. use \"\\n\" if you want a newline to be printed.\n\
  1812. Since leading and trailing whitespace are ignored in command arguments,\n\
  1813. if you want to print some you must use \"\\\" before leading whitespace\n\
  1814. to be printed or after trailing whitespace.");
  1815.   add_com ("document", class_support, document_command,
  1816.        "Document a user-defined command.\n\
  1817. Give command name as argument.  Give documentation on following lines.\n\
  1818. End with a line of just \"end\".");
  1819.   add_com ("define", class_support, define_command,
  1820.        "Define a new command name.  Command name is argument.\n\
  1821. Definition appears on following lines, one command per line.\n\
  1822. End with a line of just \"end\".\n\
  1823. Use the \"document\" command to give documentation for the new command.\n\
  1824. Commands defined in this way do not take arguments.");
  1825.  
  1826.   add_com ("source", class_support, source_command,
  1827.        "Read commands from a file named FILE.\n\
  1828. Note that the file \".gdbinit\" is read automatically in this way\n\
  1829. when gdb is started.");
  1830.   add_com ("quit", class_support, quit_command, "Exit gdb.");
  1831.   add_com ("help", class_support, help_command, "Print list of commands.");
  1832.   add_com_alias ("q", "quit", class_support, 1);
  1833.   add_com_alias ("h", "help", class_support, 1);
  1834.  
  1835.   add_cmd ("verbose", class_support, set_verbose_command,
  1836.        "Change the number of informational messages gdb prints.",
  1837.        &setlist);
  1838.   add_info ("verbose", verbose_info,
  1839.         "Status of gdb's verbose printing option.\n");
  1840.  
  1841.   add_com ("dump-me", class_obscure, dump_me_command,
  1842.        "Get fatal error; make debugger dump its core.");
  1843.  
  1844.   add_cmd ("editing", class_support, set_editing,
  1845.        "Enable or disable command line editing.\n\
  1846. Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
  1847. Without an argument, command line editing is enabled.", &setlist);
  1848.  
  1849.   add_prefix_cmd ("history", class_support, set_history,
  1850.           "Generic command for setting command history parameters.",
  1851.           &sethistlist, "set history ", 0, &setlist);
  1852.  
  1853.   add_cmd ("expansion", no_class, set_history_expansion,
  1854.        "Enable or disable history expansion on command input.\n\
  1855. Without an argument, history expansion is enabled.", &sethistlist);
  1856.  
  1857.   add_cmd ("write", no_class, set_history_write,
  1858.        "Enable or disable saving of the history record on exit.\n\
  1859. Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
  1860. Without an argument, saving is enabled.", &sethistlist);
  1861.  
  1862.   add_cmd ("size", no_class, set_history_size,
  1863.        "Set the size of the command history, \n\
  1864. ie. the number of previous commands to keep a record of.", &sethistlist);
  1865.  
  1866.   add_cmd ("filename", no_class, set_history_filename,
  1867.        "Set the filename in which to record the command history\n\
  1868.  (the list of previous commands of which a record is kept).", &sethistlist);
  1869.  
  1870.   add_prefix_cmd ("info", class_info, info_command,
  1871.           "Generic command for printing status.",
  1872.           &infolist, "info ", 0, &cmdlist);
  1873.   add_com_alias ("i", "info", class_info, 1);
  1874.  
  1875.   add_info ("editing", editing_info, "Status of command editor.");
  1876.  
  1877.   add_info ("version", version_info, "Report what version of GDB this is.");
  1878. }
  1879.